﻿using System;
using System.Collections.Generic;
using System.Text;

namespace dotHTML.HTML {
    /// <summary>
    /// Represents the result of a calculation.
    /// </summary>
    [Serializable]
    public class Output : Element {
        private ICollection<Element> _for;
        private Form _form;
        private string _name;

        /// <summary>
        /// Constructs a new instance of an Output element.
        /// </summary>
        public Output() {
            _for = new LinkedList<Element>();
        }

        /// <summary>
        /// Represents a list of elements whose inputs this output was a result
        /// of.
        /// </summary>
        public ICollection<Element> For {
            get { return _for; }
        }

        /// <summary>
        /// Determines the parent form of this output element.
        /// </summary>
        public Form Form {
            get { return _form; }
            set { _form = value; }
        }

        /// <summary>
        /// Determines the name of this output element.
        /// </summary>
        public string Name {
            get { return _name; }
            set { _name = value; }
        }

        /// <summary>
        /// Gets a formatted string of all child elements of this element.
        /// </summary>
        /// <param name="sb">The StringBuilder to write this element's child
        /// data to.</param>
        protected override void WriteLocalAttributes(StringBuilder sb) {
            string buf = "";
            foreach (Element e in _for) {
                buf += e.ID + ' ';
            }
            if (_for.Count != 0) {
                AttributeWriter.Write(sb, buf, "for");
            }
            if (_form != null) {
                AttributeWriter.Write(sb, _form.ID, "form");
            }
            AttributeWriter.Write(sb, _name, "name");
        }
    }
}
